//
//  ViewController.swift
//  swift3.0 小项目练习
//
//  Created by 龚昕炜 on 16/7/27.
//  Copyright © 2016年 龚昕炜. All rights reserved.
//

import UIKit

class ViewController: UITableViewController {
    
    var dataArrayM = [Person]()

    override func viewDidLoad() {
        super.viewDidLoad()

        loadData { (array) in
            print(array)
            
            self.dataArrayM += array
            
            self.tableView.reloadData()
        }
        
    }

    func loadData(finish: (array: [Person])->()) {
    
        DispatchQueue.global().async { 
            
            Thread.sleep(forTimeInterval: 1.0)
            
            var arrayM = [Person]()
            
            for i in 0..<20 {
                
                let p = Person()
                
                p.name = "IOS - \(i)"
                p.phone = "1860" + String(format: "%06d", arc4random_uniform(100000))
                p.title = "BOSS- \(i)"
                
                arrayM.append(p)
            }
            
            DispatchQueue.main.async(execute: { 
                finish(array: arrayM)
            })
        }
    }
    
    override func prepare(for segue: UIStoryboardSegue, sender: AnyObject?) {
        
        let vc = segue.destinationViewController as! DetailViewController
        
        if let indexPath = sender as? IndexPath {
            
            vc.person = dataArrayM[indexPath.row]
            
            vc.complitionCallBack = {
//                self.tableView.reloadData()
                self.tableView.reloadRows(at: [indexPath], with: .automatic)
            }
        } else {
            
            /** vc -引用-> complitionCallBack闭包 -引用-> vc  (循环引用) */
//            weak var weakVc = vc
            vc.complitionCallBack = { [weak vc] in
            
                guard let p = vc?.person else {
                    return
                }
                
                self.dataArrayM.insert(p, at: 0)
                
                self.tableView.reloadData()
            }
            
        }
    }
    
    @IBAction func addContact(_ sender: UIBarButtonItem) {
        performSegue(withIdentifier: "list2detail", sender: nil)
    }
    
}


extension ViewController {
    
    override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return dataArrayM.count
    }
    
    override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        
        let cell = tableView.dequeueReusableCell(withIdentifier: "CellId", for: indexPath)
        
        cell.textLabel?.text = dataArrayM[indexPath.row].name
        cell.detailTextLabel?.text = dataArrayM[indexPath.row].phone
        
        return cell
    }
    
    override func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        
        tableView.deselectRow(at: indexPath, animated: true)
        
        performSegue(withIdentifier: "list2detail", sender: indexPath)
    }
}
